#!/bin/bash

# rclone安装脚本
# 用于在Linux系统上安装rclone

set -e

echo "开始安装rclone..."

# 检查是否以root权限运行
if [[ $EUID -eq 0 ]]; then
   echo "请不要以root用户运行此脚本"
   exit 1
fi

# 检查系统架构
ARCH=$(uname -m)
echo "系统架构: $ARCH"

# 下载并安装rclone
echo "下载rclone..."
if command -v curl &> /dev/null; then
    curl -O https://downloads.rclone.org/rclone-current-linux-$ARCH.zip
elif command -v wget &> /dev/null; then
    wget https://downloads.rclone.org/rclone-current-linux-$ARCH.zip
else
    echo "错误: 未找到curl或wget命令"
    exit 1
fi

# 解压文件
echo "解压文件..."
unzip rclone-current-linux-$ARCH.zip

# 获取解压后的目录名
RCLONE_DIR=$(ls -d rclone-*-linux-$ARCH)

# 进入目录
cd $RCLONE_DIR

# 复制二进制文件到系统路径
echo "安装rclone..."
sudo cp rclone /usr/local/bin/
sudo chown root:root /usr/local/bin/rclone
sudo chmod 755 /usr/local/bin/rclone

# 安装man页面
sudo mkdir -p /usr/local/share/man/man1
sudo cp rclone.1 /usr/local/share/man/man1/
sudo mandb

# 创建配置目录
mkdir -p ~/.config/rclone

# 返回上级目录
cd ..

# 清理临时文件
echo "清理临时文件..."
rm -rf rclone-current-linux-$ARCH.zip $RCLONE_DIR

# 验证安装
echo "验证安装..."
rclone --version

echo "rclone安装完成!"